package org.authsum.stitches.wicket.contentdef.detail;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.authsum.stitches.external.*;
import org.authsum.stitches.services.StitchesService;
import org.authsum.stitches.wicket.DetailPanel;


import org.authsum.stitches.wicket.attribute.AttributesPanel;
import org.authsum.stitches.wicket.attribute.detail.AttributePanel;
import org.authsum.stitches.wicket.contentdef.*;

import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;


public class ContentDefPanel  extends DetailPanel {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;



	private static Log logger = LogFactory.getLog(ContentDefPanel.class);

	private ContentDefHolder contentDefHolder = null;
	
	public ContentDefPanel(String id,ContentDefHolder contentDefHolder,ContentDefsPanel contentDefsPanel) {
		super(id);
		this.setOutputMarkupId(true);
		System.out.println("ContentDefDetailPanel");
		this.contentDefHolder = contentDefHolder;

		form = new Form("detailForm", new CompoundPropertyModel(contentDefHolder));
		add(form);
		addSaveButtons();
		form.add(newRequiredTextField("name", 255));


		
		
		TextArea contentDefDescription = new TextArea("description");
		form.add(contentDefDescription);
		nameModel= new PropertyModel(contentDefHolder,"name");
		nameLabel = new Label("nameLabel",nameModel);
		add(nameLabel);

		
		PropertyModel pm = new PropertyModel(contentDefHolder,"id");
		Label objectId = new Label("objectId", new PropertyModel(contentDefHolder,"id"));
		add(objectId);
		
		
		AttributesPanel attributesPanel  = new AttributesPanel("attributesPanel",contentDefHolder,contentDefsPanel);
		add(attributesPanel);
		
		
		
		attributeModal = new ModalWindow("attributeModal");
		add(attributeModal);
		//String id, final RoadTrip roadTrip, final RoadTripDay roadTripDay, RoadtripDetailPanel rtdp,DmiPanel dmiPanel
		attributeModal.setContent(	new AttributePanel(attributeModal.getContentId(), contentDefHolder,attributeModal,this,contentDefsPanel));
		attributeModal
				.setTitle("Add an attribute to your content definition");
		//daysModal.setCookieName("daysModal");

		attributeModal
				.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {
					private static final long serialVersionUID = 1L;

					public boolean onCloseButtonClicked(
							AjaxRequestTarget target) {
						logger.debug("attributeModal, onCloseButtonClicked");
						return true;
					}
				});

		attributeModal
				.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
					private static final long serialVersionUID = 1L;

					public void onClose(AjaxRequestTarget target) {
						logger.debug("attributeModal, setWindowClosedCallback");
					}
				});
				

		
		
		AjaxLink addAttributeLink = (new AjaxLink("addAttributeLink") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				attributeModal.show(target);

			}

		});
		add(addAttributeLink);
		
		postInit();
		
		
		
	}

	ModalWindow attributeModal = new ModalWindow("attributeModal");

	@Override
	protected void saveDetailObject(AjaxRequestTarget target) {
		//need to save the contentDefHolder
		ContentDefRequest contentDefRequest = new ContentDefRequest();
		contentDefRequest.setContentDefHolder(contentDefHolder);

		System.out.println("contentDefHolder.getDescription()=" + contentDefHolder.getDescription());
		

		ContentDefResponse contentDefResponse = getStitchesService().saveContentDef(contentDefRequest);
		//contentDefResponse.getContentDefHolders().getContentDefHolder().
		if (contentDefResponse.getContentDefHolders().getContentDefHolder() == null) {
			System.out.println("Contentdefholders is nul");
		} else {
			this.contentDefHolder = contentDefResponse.getContentDefHolders().getContentDefHolder().get(0);
		}

		
		
		//let's refresh the screen
		info("Content Definition Saved");
		target.addComponent(this);
		
		
		
	}



	@Override
	protected List<String> getValidity() {
		List<String> list = new ArrayList<String>();
		return list;
	}

	
}
